﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.Training.Presenter.ViewInterfaces;
using Truextend.Training.Business;
using Truextend.Training.Model.Entities;
using Truextend.Training.Service;

namespace Truextend.Training.Presenter
{
    public class FormCoursePresenter
    {
        private IFormCourseView view;
        public long courseId = 0;
        private PageMode action;

        public FormCoursePresenter(IFormCourseView view, string action)
        {
            if (view == null) throw new ArgumentNullException("View may not be null");
            this.view = view;
            this.action = (action == "create") ? PageMode.CREATE : PageMode.UPDATE;
        }

        public void InitView()
        {
            try
            {
                view.Message = "";

                if (action == PageMode.CREATE)
                {
                    view.Title = "Add new course";
                }
                else
                {
                    view.Title = "Edit course";
                }
            }
            catch (Exception e)
            {
                view.Message = e.Message;
            }

        }

        public void LoadDataCourse()
        {
            if (PageMode.UPDATE == action)
            {
                //view.CourseToSave = CourseControl.GetCourseById(courseId);
                view.CourseToSave = new CourseService().GetCourseById(courseId);
            }
        }

        public void SaveCourse()
        {
            try
            {
                if (action == PageMode.CREATE)
                {
                    //CourseControl.AddCourse(view.CourseToSave);
                    new CourseService().AddCourse(view.CourseToSave);
                }
                else
                {
                    Course course = view.CourseToSave;
                    course.CourseId = courseId;
                    //CourseControl.EditCourse(course);
                    new CourseService().EditCourse(course);
                }
            }
            catch (Exception e)
            {
                view.Message = e.Message;
            }
        }

        private enum PageMode
        { 
            CREATE, UPDATE
        }
    }
}
